package com.example.chapter04;

/**
 * Math.random()
 * @date 2024/4/29
 */
public class RandomBounds {
    public static void main(String[] args) {
        timedAbort(3);
        String lower = "lower";
        switch(lower) {
            case "lower":
                while(Math.random() == 0)
                    // 持续尝试
                    System.out.println("Produced 0.0!");
                break;
            case "upper":
                while(Math.random() == 1)
                    // 持续尝试
                    System.out.println("Produced 1.0!");
                break;
            default:
                System.out.println("Usage:");
                System.out.println("\tRandomBounds lower");
                System.out.println("\tRandomBounds upper");
                System.exit(1);
        }
    }
    public static void timedAbort(int seconds) {
        new Thread(() -> {
            try {
                Thread.sleep(seconds * 1000);
                System.out.println("Aborting");
                System.exit(1);
            } catch(InterruptedException e) {
                System.out.println("sleep() interrupted");
            }
        }).start();
    }
}
